<template>
	<view class="sendBox">
		<view class="tips">只有通过认证的用户才可以发布专利作品</view>
		<view class="content">
			<view class="row">
			    <text class="tit">真实姓名:</text>
			    <input class="input" type="text" v-model="info.firmName" placeholder="请输入" placeholder-class="placeholder" />
			</view>
			<view class="row">
			    <text class="tit">身份证号:</text>
			    <input class="input" type="text" v-model="info.idCard" placeholder="请输入" placeholder-class="placeholder" />
			</view>
			<view class="row">
			    <text class="tit">验证码:</text>
			    <input class="input" type="text" v-model="info.code" placeholder="请输入" placeholder-class="placeholder" />
				<button class="code" @click="getVerificationCode" :disabled="disabled">{{ codeMsg }}</button>
			</view>
			
			<button class="add-btn" @click="confirm">提交</button>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				info:{
					firmName:"",
					idCard:"",
					code:""
				},
				codeMsg: '获取验证码',
				disabled:false,
				userinfo:null
			}
		},
		mounted() {
			let id = uni.getStorageSync('userId')
			this.$http.get('/userCenter/getUserInfoById', {id:id}).then(res => {
				if (res.code == '0') {
					this.userinfo = res.data
				}
			})
		},
		methods:{
			//验证真实姓名
			valideRealName(){
				var regName =/^[\u4e00-\u9fa5]{2,4}$/; 
				if (this.info.firmName == null || this.info.firmName == '') {
					uni.showToast({
						title: '真实姓名不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (!regName.test(this.info.firmName)) {
					uni.showToast({
						title: '真实姓名格式错误',
						icon: 'none',
						duration: 2000
					});
					return false;
				}
			},
			//验证身份证号
			validePhoneNumber() {
				var myreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
				if (this.info.idCard == null || this.info.idCard == '') {
					uni.showToast({
						title: '身份证号码不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (!myreg.test(this.info.idCard)) {
					uni.showToast({
						title: '身份证号格式错误',
						icon: 'none',
						duration: 2000
					});
					return false;
				}
			},
			//验证验证码
			getVerificationCode() {
					let phone = uni.getStorageSync('phone')
					let count = 60;
					this.codeMsg = count + 's';
					let timer = setInterval(() => {
						if (count <= 1) {
							clearInterval(timer);
							this.codeMsg = '获取验证码';
							this.disabled = false;
						} else {
							count--;
							this.codeMsg = count + 's';
							//获取验证码
			
							this.disabled = true;
						}
					}, 1000);
					this.$http.get('/sms/getSmsCode', { phone: phone, type: 1 }).then(res => {
						console.log(res);
						if (res.code == '0') {
							uni.showToast({
								title: '验证码已发送',
								icon: 'success'
							});
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					});
				
			},
			//提交认证
			confirm(){
				var nameFlag = this.valideRealName();
				if(nameFlag!=false){
					var flag = this.validePhoneNumber();
					if (flag != false) {
						if (this.info.code == null || this.info.code == '') {
							uni.showToast({
								title: '验证码不能为空',
								icon: 'none',
								duration: 2000
							});
							return false;
						}else{
							//调接口
							this.sendValid();
							
						}
						
					}
				}
				
				
			},
			//认证接口
			sendValid(){
				let params = {
					  code: this.info.code,
					  idCard: this.info.idCard,
					  realName: this.info.firmName,
					  type: 1
					}
				this.$http.post('/userCenter/userCertification', params).then(res => {
					if (res.code == '0') {
						console.log(res.data)
						// uni.navigateTo({
						// 	url:'/pages/status/wait?type=rz'
						// })
						
						
						
							if(!this.userinfo.isRealName){
								this.$SysCache.put("linkUrl",res.data.url)
								uni.navigateTo({
									url:`/pages/person/userAuth`
								})
							}else{
								uni.showToast({
									icon:"none",
									title:"你已实名认证，无需再次操作！"
								})
							}
						
						
					}else{
						uni.showToast({
							icon:"none",
							title:res.msg
						})
					}
				})
			}
		}
	}
		
</script>

<style scoped lang="scss">
	.sendBox{
		
		.tips{
			padding:25upx 20upx ;
			font-size: 24upx;
			color: #FD580A;
			background:rgba(253,88,10,0.1);
			justify-content: space-between;
			align-items: center;
			margin-bottom: 20upx;
		}
		.content{
			height: 100%;
			background: #fff;
			.row {
			    display: flex;
			    align-items: center;
			    position: relative;
			    margin: 0 30upx;
			    height: 110upx;
			    background: #fff;
				border-bottom: 1upx solid rgba(238,238,238,1);
			    .tit {
			        flex-shrink: 0;
			        width: 150upx;
			        font-size: 30upx;
					color: #22222A;
			    }
				.code{
					width: 140upx;
					height: 48upx;
					background: rgba(253, 88, 10, 1);
					border-radius: 8upx;
					font-size: 24upx;
					color: #fff;
					text-align: center;
					line-height: 48upx;
					padding: 0;
				}
			    .input {
			        flex: 1;
			        font-size: 30upx;
			
			    }
			
			    .icon-shouhuodizhi {
			        font-size: 36upx;
			
			    }
			}
			.add-btn {
			    width:600upx;
			    height:80upx;
			    background:#FD580A;
			    border-radius:8upx;
				font-size: 30upx;
				color: #fff;
				text-align: center;
				line-height: 80upx;
				margin-top: 178upx;
			}
		}
	}
</style>
